package com.shop.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.entity.dto.AdminOrders.AdminOrdersGetDetailDto;
import com.shop.entity.pojo.Orders;
import com.shop.entity.pojo.OrdersDetail;
import com.shop.admin.mapper.OrdersDetailMapper;
import com.shop.admin.mapper.OrdersMapper;
import com.shop.admin.service.OrdersDetailService;
import com.shop.utils.common.enums.Code;
import com.shop.utils.common.enums.Message;
import com.shop.utils.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OrdersDetailServiceImpl extends ServiceImpl<OrdersDetailMapper, OrdersDetail>
    implements OrdersDetailService{

    @Autowired
    private OrdersDetailMapper ordersDetailMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Result<List<OrdersDetail>> getOrdersDetail(AdminOrdersGetDetailDto dto) {
        long start = System.currentTimeMillis();
        log.info("后台管理-订单详情");
        if (dto==null) {
            log.info("dto为空");
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        if (dto.getOrdersId()==null) {
            log.info("未获取到ordersId");
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        Orders orders = ordersMapper.selectById(dto.getOrdersId());
        if (orders==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        String key=Message.Admin_key_Orders_Detail+dto.getOrdersId();
        List<OrdersDetail> check = (List<OrdersDetail>) redisTemplate.opsForValue().get(key);
        if (check!=null&&check.size()>0) {
            log.info("浏览缓存");
            long end = System.currentTimeMillis();
            log.info("后台管理获取订单详情进程完成，总耗时:{}",(end-start));
            return Result.okResult(check);
        }
        List<OrdersDetail> detailList = ordersDetailMapper.getList(orders.getOrdersId());
        if (CollectionUtils.isEmpty(detailList) ||detailList.size()== Message.Check_Int_Null) {
            log.info("内部错误无法获取到订单详情");
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        long end = System.currentTimeMillis();
        log.info("后台管理获取订单详情进程完成，总耗时:{}",(end-start));
        return Result.okResult(detailList);
    }
}




